package com.ease.framework.gef4g {
   import com.ease.util.collections.HashMap;
   import com.ease.util.collections.Map;
   import com.ease.framework.figures.ContainerFigure;
   import com.ease.framework.layers.ConnectionLayer;
   import com.ease.framework.layers.DraggingLayer;
   import com.ease.framework.layers.GridLayer;
   import com.ease.framework.layers.HandlesLayer;
   import com.ease.framework.layers.LabelLayer;
   import com.ease.framework.layers.LaneLayer;
   import com.ease.framework.layers.Layer;
   import com.ease.framework.layers.MaskLayer;
   import com.ease.framework.layers.PrimaryLayer;
   import com.ease.framework.layers.SnapLayer;
   import com.ease.framework.workbench.viewer.IViewer;

   import mx.core.ScrollPolicy;

   /**
    * 只是图形化的Content viewport.
    *
    * 本viewport包含了几个Layer，每个Layer包含不同的Figure.
    */
   public class GraphicalViewport extends ContainerFigure implements IViewport {

      public static const LAYER_MASK:String = "Mask";
      public static const LAYER_LABEL:String = "Label"; //放置用于编辑label的label editor
      public static const LAYER_DRAGGING:String = "Dragging"; //放置在拖拽过程中各个图形的替身，每次拖拽完毕后会清空
      public static const LAYER_HANDLES:String = "Handles"; //放置Figure的锚点、Bends、Handles点.
      public static const LAYER_PRIMARY:String = "Primary"; //放置主要的Node和Container图形层.
      public static const LAYER_CONNECTION:String = "Connection"; //放置Edge层
      public static const LAYER_SNAP:String = "Snap"; //放置Snap层
      public static const LAYER_LANE:String = "Lane"; //放置Lane层
      public static const LAYER_GRID:String = "Grid"; //放置Grid层

      protected var viewer:IViewer;

      private var layerMaps:Map = new HashMap();

      public function GraphicalViewport() {
         super();

         var gridLayer:GridLayer = new GridLayer();
         gridLayer.percentHeight = 100;
         gridLayer.percentWidth = 100;
         addChild(gridLayer);
         gridLayer.visible = true;

         var snapLayer:SnapLayer = new SnapLayer();
         snapLayer.percentHeight = 100;
         snapLayer.percentWidth = 100;
         addChild(snapLayer);

         var primaryLayer:PrimaryLayer = new PrimaryLayer();
         primaryLayer.percentHeight = 100;
         primaryLayer.percentWidth = 100;
         addChild(primaryLayer);

         var laneLayer:LaneLayer = new LaneLayer();
         laneLayer.percentWidth = 100;
         laneLayer.percentHeight = 100;
         addChild(laneLayer);

         var edgeLayer:ConnectionLayer = new ConnectionLayer();
         edgeLayer.percentWidth = 100;
         edgeLayer.percentHeight = 100;
         addChild(edgeLayer);

         var handlesLayer:HandlesLayer = new HandlesLayer();
         handlesLayer.percentHeight = 100;
         handlesLayer.percentWidth = 100;
         addChild(handlesLayer);

         var draggingLayer:DraggingLayer = new DraggingLayer();
         draggingLayer.percentHeight = 100;
         draggingLayer.percentWidth = 100;
         addChild(draggingLayer);

         var labelLayer:LabelLayer = new LabelLayer();
         labelLayer.percentHeight = 100;
         labelLayer.percentWidth = 100;
         addChild(labelLayer);

         var maskLayer:MaskLayer = new MaskLayer();
         maskLayer.percentHeight = 100;
         maskLayer.percentWidth = 100;
         addChild(maskLayer);

         layerMaps.put(LAYER_CONNECTION, edgeLayer);
         layerMaps.put(LAYER_PRIMARY, primaryLayer);
         layerMaps.put(LAYER_MASK, maskLayer);
         layerMaps.put(LAYER_LABEL, labelLayer);
         layerMaps.put(LAYER_LANE, laneLayer);
         layerMaps.put(LAYER_DRAGGING, draggingLayer);
         layerMaps.put(LAYER_HANDLES, handlesLayer);
         layerMaps.put(LAYER_GRID, gridLayer);

         horizontalScrollPolicy = ScrollPolicy.OFF;
         verticalScrollPolicy = ScrollPolicy.OFF;
         clipContent = true;
      }

      public function getLayer(layerName:String):Layer {
         return layerMaps.getObject(layerName) as Layer;
      }

      public function get contentLayer():Layer {
         return getLayer(LAYER_PRIMARY);
      }

      public function get connectionLayer():Layer {
         return getLayer(LAYER_CONNECTION);
      }

      public function get maskLayer():Layer {
         return getLayer(LAYER_MASK);
      }

      public function get labelLayer():Layer {
         return getLayer(LAYER_LABEL);
      }

      public function get laneLayer():Layer {
         return getLayer(LAYER_LANE);
      }

      public function get draggingLayer():Layer {
         return getLayer(LAYER_DRAGGING);
      }

      public function get handlesLayer():Layer {
         return getLayer(LAYER_HANDLES);
      }

      public function get primaryLayer():Layer {
         return getLayer(LAYER_PRIMARY);
      }

      public function getViewer():IViewer {
         return viewer;
      }

      public function setViewer(newViewer:IViewer):void {
         if (viewer == newViewer)
            return;
         //此处可以做一些unregister操作。暂无
         viewer = newViewer;
         //此处可以做一些register操作。暂无
      }

      public function clear():void {
         contentLayer.removeAllChildren();
         connectionLayer.removeAllChildren();
      }
   }
}